package org.southyin.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PersonMain2 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        Class cls = Person2.class;
        Field name = cls.getDeclaredField("name");
        // 暴力反射
        name.setAccessible(true);
        System.out.println(name.getName());
        Person2 p = new Person2();
        name.set(p,"tom");
        System.out.println(p.getName());

        Constructor constructor = cls.getDeclaredConstructor(String.class, Integer.class);
        constructor.setAccessible(true);
        Person2 ella = (Person2) constructor.newInstance("ella", 30);

        Method test = cls.getDeclaredMethod("test");
        test.setAccessible(true);
        test.invoke(ella);
    }
}
